package com.youkeyi.ddy.cloud.common.utils;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.apache.commons.lang3.StringUtils;

/**
 * AESUtils
 *
 * @Author liuxiawang
 * @Date 8/2/23 9:53 AM
 * @Location shenzhen.china
 */
public class AESUtils {
    private static final SymmetricCrypto AES;

    static {
        // 随机生成密钥
        // byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        byte[] key = HexUtil.decodeHex("f552f1b3e8a5087bd3d85787db615f09");
        //构建
        AES = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
    }

    /**
     * 加密为16进制表示
     */
    public static String encrypt(String value) {
        if (StringUtils.isBlank(value)) {
            return value;
        }
        return AES.encryptHex(value);
    }

    /**
     * 解密为字符串
     */
    public static String decrypt(String value) {
        if (StringUtils.isBlank(value)) {
            return value;
        }
        return AES.decryptStr(value, CharsetUtil.CHARSET_UTF_8);
    }

    public static void main(String[] args) {
        System.err.println(AESUtils.encrypt("测试"));
        System.err.println(AESUtils.decrypt(AESUtils.encrypt("测试")));
    }
}
